//
//  KVOController.m
//  StudyingDemo
//
//  Created by GuoMS on 2018/6/9.
//  Copyright © 2018年 gms. All rights reserved.
//

#import "KVOController.h"
#import "Person.h"
#import "Dog.h"
#import "NSObject+GMKVO.h"

@interface KVOController ()

@property (strong, nonatomic)Person *p;

@property (strong, nonatomic) UILabel *txtlabel;

@end

@implementation KVOController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"KVO";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.txtlabel = [[UILabel alloc]init];
    self.txtlabel.font = [UIFont systemFontOfSize:16];
    self.txtlabel.textColor = [UIColor redColor];
    self.txtlabel.textAlignment = NSTextAlignmentCenter;
    self.txtlabel.numberOfLines = 0;
    self.txtlabel.frame = CGRectMake(0, 0,kScreenWidth, kScreenHeight);
    [self.view addSubview:self.txtlabel];
    
    [self addObserverAction];
    
    
}


- (void)addObserverAction {
    
    /* options 参数定义 可以多个用|隔开
     * NSKeyValueObservingOptionNew  返回新值
     * NSKeyValueObservingOptionOld  返回旧值
     * NSKeyValueObservingOptionInitial 注册的时候会发一次通知，改变也会发通知
     * NSKeyValueObservingOptionPrior 改变之前发一次，改变之后发一次
     */
    _p = [Person new];
    
     //监听key
//    [_p addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    //监听子对象的key
//    [_p addObserver:self forKeyPath:@"dog.age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
//    //监听array类型的key
//    [_p addObserver:self forKeyPath:@"dataArr" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    //监听array的个数 不能监听到数组的个数变化
//    [_p.dataArr addObserver:self forKeyPath:@"dataArr.count" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    //监听整个对象
//    [_p addObserver:self
//         forKeyPath:@"dog"
//            options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
//            context:nil];
    
    //自己实现监听
    [_p GM_addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}



/**
 代理方法

 @param keyPath 监听的key
 @param object 监听的对象
 @param change 变化的值
 @param context 上下文
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    NSString *changeStr = [NSString stringWithFormat:@"\n keyPath:%@\n object:%@\n change:%@",keyPath,object,change];
    NSLog(@"%@",changeStr);

    self.txtlabel.text = [NSString stringWithFormat:@"监听key:%@ \n 监听对象:%@ \n 变化字典:%@",keyPath,object,change];
//kind 的类型
//    NSKeyValueChangeSetting = 1,  set值设置
//    NSKeyValueChangeInsertion = 2, 插入变化
//    NSKeyValueChangeRemoval = 3,  移除变化
//    NSKeyValueChangeReplacement = 4, 替换变化
//监听name对象
//keyPath:name
//object:<Person: 0x604000232300>
//change:{
//    kind = 1;
//}
    
//keyPath:dog.age
//object:<Person: 0x6000002210e0>
//change:{
//    kind = 1;
//    new = 8;
//    old = 7;
//}

    
//监听arr对象
//keyPath:dataArr
//object:<Person: 0x60400024e430>
//change:{
//    indexes = "<NSIndexSet: 0x60400003b940>[number of indexes: 1 (in 1 ranges), indexes: (35)]";
//    kind = 2;
//    new =     (
//               d
//               );
//}

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    
//    //kvo是监听set方法，需要结合KVC
//    [_p.dataArr addObject:@"dfsd"];
    [[_p mutableArrayValueForKey:@"dataArr"]addObject:@"d"];
    
//    _p.age++;
//    static int age = 0;
//    age ++;
//    _p.age++;
//    _p.dog.age++;
    
//    age ++;
//    _p.age = age;
//当设置为实体+ (BOOL)automaticallyNotifiesObserversOfAge 为YES时，需要手动触发KVO
//    [_p willChangeValueForKey:@"age"];
//    _p.age = age;
//    [_p didChangeValueForKey:@"age"];
}

@end
